<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/9
     * Time: 上午10:31
     */

    namespace app\store\api;


    use app\common\controller\API;
    use app\store\model\AddressModel;
    use think\Db;
    use think\exception\HttpResponseException;

    class Address extends API
    {

        /**
         * 地址列表
         * api POST /store/Address/lists
         *
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function lists()
        {

            $addresses = AddressModel::where(['member_id' => $this->member_id])->select();

            $this->result($addresses);
        }

        /**
         * 获取默认地址
         * api POST /store/Address/defautl
         *
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function defautl()
        {

            $def = AddressModel::where(['member_id' => $this->member_id,'is_default' => 1])->find();

            if (!$def)
            {
                $def = AddressModel::where(['member_id' => $this->member_id])->find();
                if ($def)
                {
                    $def->is_default = 1;
                    $def->save();
                    $this->result($def);
                }
                $this->result([]);

            }
            else
            {
                $this->result($def);
            }

        }

        /**
         * 获取地址
         * api POST /store/Address/get
         *
         * @param int $id 地址ID
         *
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function get()
        {

            $id  = $this->request->param('id');
            $def = AddressModel::where(['id' => $id,'member_id' => $this->member_id])->find();
            $this->result($def);

        }

        /**
         * 添加地址
         * api POST /store/Address/add
         *
         * @param array   $address_area   地址数组按顺序(省，市，区，县 )
         * @param string  $name           收货人
         * @param string  $mobile         手机号
         * @param string  $address_detail 详细
         * @param integer $is_default     是否设置为默认地址(可选)
         *
         * @return string $code  200-成功500-失败
         */
        public function add()
        {

            $data = $this->request->param();

            $data['member_id'] = $this->member_id;

            $this->check_validate($data,'Address.add');

            if ($data['is_default'] == 1)
            {
                AddressModel::update(['is_default' => 0],['member_id' => $this->member_id]);
            }
            if (AddressModel::create($data))
            {
                $this->success('添加成功！');
            }
            else
            {
                $this->error('添加失败');
            }
        }

        /**
         * 设置默认地址
         * api POST  /store/Address/set_default
         *
         * @param int id 地址ID
         * @param int is_default 是否默认(1为默认否则为0)
         */
        public function set_default()
        {

            $data              = $this->request->param();
            $data['member_id'] = $this->member_id;
            if (isset($data['is_default']) && $data['is_default'] == 1)
            {
                try
                {
                    Db::startTrans();
                    AddressModel::update(['is_default' => 0],['member_id' => $this->member_id]);
                    AddressModel::update(['is_default' => $data['is_default']],[
                        'id'        => $data['id'],
                        'member_id' => $this->member_id
                    ]);
                    Db::commit();
                    return $this->success('设置成功！');
                }
                catch (HttpResponseException $exception)
                {
                    Db::rollback();
                    return $this->error('设置失败！','',$exception);
                }


            }
        }

        /**
         * 更新地址
         * api POST /store/Address/update
         *
         * @param integer $id             地址ID
         * @param array   $address_area   地址数组按顺序(省，市，区，县 )
         * @param string  $name           收货人
         * @param string  $mobile         手机号
         * @param string  $address_detail 详细
         * @param integer $is_default     是否设置为默认地址(可选)
         *
         * @return string $code  200-成功500-失败
         */
        public function update()
        {

            $data = $this->request->param();

            $this->check_validate($data,'Address.add');


            $re = AddressModel::update($data,['member_id' => $this->member_id,'id' => $data['id']]);
            if ($re)
            {
                $this->success('更新成功！');
            }
            else
            {
                $this->error('更新失败');
            }
        }

        /**
         * 删除地址
         * api POST /store/Address/del
         *
         * @param  integer $ids 商品ID，逗号分隔-例如：1，2，3，4
         *
         * @return string $code  200-成功500-失败
         * @return string $msg  错误说明
         * @return string $data[]
         */
        public function del()
        {

            $ids = $this->request->param('ids',false);
            if (isset($ids))
            {
                if (AddressModel::whereIn('id',$ids)->delete())
                {
                    $this->success('删除成功！');
                }
                else
                {
                    $this->error('删除失败');
                }

            }
            else
            {
                $this->error('参数错误！');
            }
        }

    }